package com.sgb.kjwl.view.ui.activity.self;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMoreInfoBinding;
import com.sgb.kjwl.model.entity.self.CarriagePushEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.InvoiceRateEntity;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.utils.PickerHelper;
import com.sgb.kjwl.viewmodel.self.carMakers.EditMoreInfoViewModel;
import com.sgb.link.router.PathLogistics;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.dialog.RoundDialog;

import java.util.List;

/**
 * @author: 王彬臣
 * @date:
 * @Description: 推送承运更多信息
 */
@Route(path = PathLogistics.EditMoreInfoActivity)
public class EditMoreInfoActivity extends AppActivity {
    @Autowired(name = RouterKey.DATA, desc = "数据实体类")
    public CarriagePushEntity mEntity;
    //
    private EditMoreInfoViewModel mViewModel;
    private ActivityMoreInfoBinding mBinding;
    //选择器
    private OptionsPickerView<GoodsInfoTypeEntity3> mPayPicker;
    private OptionsPickerView<GoodsInfoTypeEntity3> mBargainPicker;
    private OptionsPickerView<InvoiceRateEntity> mInvoicePicker;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        mViewModel = new ViewModelProvider(this).get(EditMoreInfoViewModel.class);
    }

    @Override
    protected void init() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_more_info);
        mBinding.setLifecycleOwner(this);
        mBinding.includeTitle.titleView.setText("更多信息");
        initEvent();
        initData();
    }

    private void initEvent() {
        mBinding.setOnClick(this);
        mBinding.includeTitle.ivBack.setOnClickListener(this);
        //接口回调
        mViewModel.ldPayType.observe(this, this::syncPayType);
        mViewModel.ldBargain.observe(this, this::syncBargain);
        mViewModel.ldInvoiceRateEntity.observe(this, this::syncInvoice);
    }

    private void initData() {
        syncData();
        mViewModel.getPayType();
        mViewModel.getBargain();
        mViewModel.getInvoices();
    }

    /**
     * 同步数据
     */
    private void syncData() {
        //todo
        mBinding.tvPushCarriagePay.setText(DefaultUtil.getString(mEntity.getPayType()));
        mBinding.tvPushCarriageBargain.setText(CargoConstant.getBargainStr(mEntity.getBargain()));
        mBinding.etPushCarriageMark.setText(DefaultUtil.getString(mEntity.getNote()));
        checkInvoiceInfo();
    }

    /**
     * 设置付款方式
     */
    private void syncPayType(List<GoodsInfoTypeEntity3> list) {
        if (list == null) return;
        mPayPicker = PickerHelper.with(this).getCommonPicker("选择付款方式", list, (options1, options2, options3, v) -> {
            GoodsInfoTypeEntity3 entity = list.get(options1);
            //设置数据
            mEntity.setPayType(entity.getName());
            //设置回显
            mBinding.tvPushCarriagePay.setText(DefaultUtil.getString(entity.getName()));
        });
    }

    /**
     * 设置可议价
     */
    private void syncBargain(List<GoodsInfoTypeEntity3> list) {
        if (list == null) return;
        mBargainPicker = PickerHelper.with(this).getCommonPicker("可否议价", list, (options1, options2, options3, v) -> {
            GoodsInfoTypeEntity3 entity = list.get(options1);
            //设置数据
            mEntity.setBargain(NumberUtil.convertToInt(entity.getNo()));
            //设置回显
            mBinding.tvPushCarriageBargain.setText(DefaultUtil.getString(entity.getName()));
        });
    }

    /**
     * 设置发票要求
     */
    private void syncInvoice(List<InvoiceRateEntity> list) {
        if (list == null) return;
        mInvoicePicker = PickerHelper.with(this).getCommonPicker("选择发票要求", list, (options1, options2, options3, v) -> {
            InvoiceRateEntity entity = list.get(options1);
            //设置数据
            if (!TextUtils.isEmpty(entity.getRate())) {
                mEntity.setRate(entity.getRate());
            }
            if (!TextUtils.isEmpty(entity.getNo())) {
                mEntity.setInvoiceNo(entity.getNo());
            }
            if (!TextUtils.isEmpty(entity.getInvoiceRequest())) {
                mEntity.setInvoice(entity.getInvoiceRequest());
            }
            //设置回显
            mBinding.tvPushCarriageInvoice.setText(DefaultUtil.getString(entity.getName()));
        });

    }

    /**
     * 发票要求信息匹配检测
     */
    private void checkInvoiceInfo() {
        if (mEntity.isInvoiceChanges()) {
            mEntity.setInvoice("");
            mEntity.setRate("");
            mEntity.setInvoiceNo("");
            showFieldAlterDialog();
        } else if (!TextUtils.isEmpty(mEntity.getInvoice()) && !TextUtils.isEmpty(mEntity.getRate()) && !TextUtils.isEmpty(mEntity.getInvoiceNo())) {
            if (mEntity.getInvoice().equals("不开发票")) {
                mBinding.tvPushCarriageInvoice.setText(mEntity.getInvoice());
            } else {
                String invoiceStr = NumberUtil.trim0(mEntity.getRate()) + "%" + mEntity.getInvoice();
                mBinding.tvPushCarriageInvoice.setText(invoiceStr);
            }
        }
    }

    /**
     * 发票要求字段调整，提示用户重新选择
     */
    private void showFieldAlterDialog() {
        new RoundDialog.Builder(this)
                .title("提示")
                .content("平台发票要求已调整，使用历史承运发布需重新选择该信息")
                .confirm("我知道了")
                .singleBtn(true)
                .confirmCallback(v -> {
                    if (mEntity != null) {
                        mEntity.setInvoiceChanges(false);
                    }
                })
                .contentGravity(Gravity.CENTER)
                .cancelable(false)
                .show();
    }

    @Override
    public void onClick(View v) {
        closeKeybord();
        if (v == mBinding.includeTitle.ivBack) {
            finish();
        } else if (v == mBinding.llPushCarriagePay) {
            if (mPayPicker != null) {
                mPayPicker.show();
            }
        } else if (v == mBinding.llPushCarriageInvoice) {
            if (mInvoicePicker != null) {
                mInvoicePicker.show();
            }
        } else if (v == mBinding.llPushCarriageBargain) {
            if (mBargainPicker != null) {
                mBargainPicker.show();
            }
        } else if (v == mBinding.tvPushCarriageConfirm) {
            clearAllFocus();
            finishWithData();
        }
    }

    private void finishWithData() {
        //设置备注信息
        mEntity.setNote(mBinding.etPushCarriageMark.getText().toString());
        Intent intent = new Intent();
        intent.putExtra(RouterKey.DATA, mEntity);
        setResult(RESULT_OK, intent);
        finish();
    }
}
